﻿/*
    Parker for Kaixin001.com 

    Copyright (C) 2008, TurboZV, turbozv@gmail.com, http://www.turbozv.com/

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Net.Json;

namespace Helper
{
    public class JsonHelper
    {
        public static int GetIntegerValue(JsonObject field)
        {
            string value = GetStringValue(field);
            if (null == value)
            {
                return -1;
            }

            try
            {
                return Int32.Parse(value);
            }
            catch (OverflowException)
            {
                return -1;
            }
            catch (FormatException)
            {
                return -1;
            }
        }

        public static string GetStringValue(JsonObject field)
        {
            if (null == field || null == field.GetValue())
            {
                return null;
            }
            string value = null;
            string type = field.GetValue().GetType().Name;

            // try to get value.
            switch (type.ToLower())
            {
                case "string":
                    value = (string)field.GetValue();
                    break;

                case "double":
                    value = field.GetValue().ToString();
                    break;

                case "boolean":
                    value = field.GetValue().ToString();
                    break;
            }

            return value;
        }
    }
}
